package com.xyzwps.benchmarks.json;

import com.xyzwps.benchmarks.util.TablePrinter;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class Measure {

    public static String measure(Runnable runnable) {
        long ts = System.currentTimeMillis();
        runnable.run();
        return System.currentTimeMillis() - ts + "ms";
    }

    public static void main(String[] args) {
        List<String> header = List.of("lib", "parse", "stringify");
        List<String> gson = List.of("gson", measure(Measure::gsonParse), measure(Measure::gsonStringify));
        List<String> fastJson = List.of("fastJson", measure(Measure::fastJsonParse), measure(Measure::fastJsonStringify));
        List<String> jackson = List.of("jackson", measure(Measure::jacksonParse), measure(Measure::jacksonStringify));
        List<String> johnzon = List.of("johnzon", measure(Measure::johnzonParse), measure(Measure::johnzonStringify));
        List<String> yasson = List.of("yasson", measure(Measure::yassonParse), measure(Measure::yassonStringify));

        List<List<String>> table = List.of(gson, fastJson, jackson, johnzon, yasson);

        TablePrinter.print(header, table);
    }

    private static final int count = 200000;
    private static final String json = "{\"asset\":123.4,\"attrs\":{\"enchanted\":true,\"rising\":\"Empire\",\"xbox\":\"veli cool\",\"idol\":\"杨超越\"},\"level\":12,\"name\":\"Ben Marker\",\"registerTime\":1595063553887,\"tags\":[\"颜狗\",\"函数式编程\",\"AI\",\"Candy\"],\"vip\":true}";

    private static final BenUser ben = new BenUser();

    static {
        ben.setAsset(BigDecimal.valueOf(123.4));
        ben.setAttrs(Map.of(
                "enchanted", true,
                "rising", "Empire",
                "xbox", "veli cool",
                "idol", "杨超越"
        ));
        ben.setLevel(12);
        ben.setName("Ben Marker");
        ben.setRegisterTime(new Date());
        ben.setTags(List.of("颜狗", "函数式编程", "AI", "Candy"));
        ben.setVip(true);
    }

    public static void fastJsonStringify() {
        for (int i = 0; i < count; i++) {
            FastJsonUtils.INSTANCE.stringify(ben);
        }
    }

    public static void gsonStringify() {
        for (int i = 0; i < count; i++) {
            GsonUtils.INSTANCE.stringify(ben);
        }
    }

    public static void jacksonStringify() {
        for (int i = 0; i < count; i++) {
            JacksonUtils.INSTANCE.stringify(ben);
        }
    }

    public static void johnzonStringify() {
        for (int i = 0; i < count; i++) {
            JohnzonUtils.INSTANCE.stringify(ben);
        }
    }

    public static void yassonStringify() {
        for (int i = 0; i < count; i++) {
            YassonUtils.INSTANCE.stringify(ben);
        }
    }


    public static void fastJsonParse() {
        for (int i = 0; i < count; i++) {
            FastJsonUtils.INSTANCE.parse(json, BenUser.class);
        }
    }

    public static void gsonParse() {
        for (int i = 0; i < count; i++) {
            GsonUtils.INSTANCE.parse(json, BenUser.class);
        }
    }

    public static void jacksonParse() {
        for (int i = 0; i < count; i++) {
            JacksonUtils.INSTANCE.parse(json, BenUser.class);
        }
    }

    public static void johnzonParse() {
        for (int i = 0; i < count; i++) {
            JohnzonUtils.INSTANCE.parse(json, BenUser.class);
        }
    }

    public static void yassonParse() {
        for (int i = 0; i < count; i++) {
            YassonUtils.INSTANCE.parse(json, BenUser.class);
        }
    }
}
